<?php


namespace app\admin\controller\mall;


use app\common\model\MallMembercard;
use app\common\model\MallMembercardxz;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * Class Goods
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="商城商品管理")
 */
class Membercardxz extends AdminController
{

    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallMembercardxz();
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index($id='')
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }

            list($page, $limit, $where) = $this->buildTableParames();
            $where['mcard_id']=$id;
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach($list as $k=>$v){
                $list[$k]['sort'] = $limit * ($page - 1) + $k + 1;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list
            ];
            return json($data);
        }
        $this->assign('cid',$id);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add($id='')
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['mcard_id'] =$id;
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }



}